﻿/*
=======================================================
    RegulatorData【数据权限】
=======================================================
*/

namespace Services
{
    public interface IRegulatorDataRepository : IRepository<RegulatorData>
    {
    }


    public class RegulatorDataRepository : BaseRepository<RegulatorData>, IRegulatorDataRepository
    {
        public RegulatorDataRepository(ISqlSugarClient dB, ICurrentUser currentUser, IQuery<RegulatorData> query)
            : base(dB, currentUser, query)
        {
        }

        #region GetPage
        public override (List<RegulatorData> items, int totalCount) GetPage(Expressionable<RegulatorData> where, int pageIndex = 1, int pageSize = 20, string orderBy = null)
        {
            var fs = _DB.Queryable<RegulatorData, Province, City, Area>
                ((x, p, c, a) => new JoinQueryInfos(
                    JoinType.Inner, x.ProvinceID == p.ProvinceID,
                    JoinType.Left, x.CityID == c.CityID,
                    JoinType.Left, x.AreaID == a.AreaID
                ))
                .Select((x, p, c, a) => new RegulatorData
                {
                    RegulatorDataID = x.RegulatorDataID.SelectAll(),
                    Province_Name = p.Name,
                    City_Name = c.Name,
                    Area_Name = a.Name,
                });

            return _query.GetPage(where, pageIndex, pageSize, orderBy, fs);
        }
        #endregion

        #region GetSearch
        public override List<RegulatorData> GetSearch(Expressionable<RegulatorData> where, string orderBy = null, int? top = null)
        {
            var fs = _DB.Queryable<RegulatorData, Province, City, Area>
               ((x, p, c, a) => new JoinQueryInfos(
                   JoinType.Inner, x.ProvinceID == p.ProvinceID,
                   JoinType.Left, x.CityID == c.CityID,
                   JoinType.Left, x.AreaID == a.AreaID
               ))
               .Select((x, p, c, a) => new RegulatorData
               {
                   RegulatorDataID = x.RegulatorDataID.SelectAll(),
                   Province_Name = p.Name,
                   City_Name = c.Name,
                   Area_Name = a.Name,
               });

            return _query.GetSearch(where, orderBy, top, fs);
        }
        #endregion

        #region Insert
        public override int Insert(RegulatorData entity)
        {
            entity.IsSystem = false;
            entity.ParentID = 0;

            int id = base.Insert(entity);

            if (!entity.CityID.HasValue)
            {
                var cityIDs = _DB.Queryable<City>().Where(x => x.ProvinceID == entity.ProvinceID).Select(x => x.CityID).ToList();
                var areas = _DB.Queryable<Area>().Where(x => cityIDs.Contains(x.CityID)).ToList();

                var list = areas.Select(x => new RegulatorData
                {
                    RegulatorID = entity.RegulatorID,
                    ProvinceID = entity.ProvinceID,
                    CityID = x.CityID,
                    AreaID = x.AreaID,
                    IsSystem = true,
                    ParentID = id
                }).ToList();

                _DB.Insertable(list).ExecuteCommand();
            }
            else if (!entity.AreaID.HasValue)
            {
                var areaIDs = _DB.Queryable<Area>().Where(x => x.CityID == entity.CityID).Select(x => x.AreaID).ToList();

                var list = areaIDs.Select(x => new RegulatorData
                {
                    RegulatorID = entity.RegulatorID,
                    ProvinceID = entity.ProvinceID,
                    CityID = entity.CityID,
                    AreaID = x,
                    IsSystem = true,
                    ParentID = id
                }).ToList();

                _DB.Insertable(list).ExecuteCommand();
            }

            return id;
        }
        #endregion

        #region Delete
        public override int Delete(int id)
        {
            _DB.Deleteable<RegulatorData>().Where(x => x.ParentID == id).ExecuteCommand();
            return base.Delete(id);
        }
        #endregion
    }
}
